********************************************************************************
* STEADY STATE VOLATILITY - RESULTS FOR BOTH P99 AND HERFINDAHL ****************
********************************************************************************
local listcontrol "1 3 5 6"
foreach cc of local listcontrol{
global version = `cc'
do ${do}/settingglobals.do

use citydata_72${version}.dta, clear

if "${var_x}"=="herfindahl_sa"{
qui: replace m_${var_x}=exp(m_${var_x})
}

qui: merge m:1 amr using steadystate${version}.dta
qui: drop _merge

ren m_${var_x} concentration 

qui: gen logsize = ln(m_empl)
qui: reg volatility_ss concentration
qui: est store m1_${version}
qui: reg volatility_ss  concentration logsize
qui: est store m2_${version}
}

esttab m1_1 m1_3 m1_5 m1_6 using ${tables}/stvalues_all1.tex, label r2 replace star(* 0.10 ** 0.05 *** 0.01)

esttab m2_1 m2_3 m2_5 m2_6 using ${tables}/stvalues_all2.tex, label r2 replace star(* 0.10 ** 0.05 *** 0.01)
********************************************************************************
* AUTOREGRESSIVE COEFFICIENT - ONLY RESULTS FOR P99 ****************************
********************************************************************************
local listcontrol "5"
foreach cc of local listcontrol{
global version = `cc'
do ${do}/settingglobals.do

use citydata_72${version}.dta, clear

if "${var_x}"=="herfindahl_sa"{
qui: replace m_${var_x}=exp(m_${var_x})
}

qui: merge m:1 amr using steadystate${version}.dta
qui: drop _merge

qui: merge m:1 amr using ${orig}/firmlevelpersistence.dta
qui: drop _merge

ren m_${var_x} concentration 

qui: gen logsize = ln(m_empl)
reg ar  concentration
qui: est store m1_${version}
reg ar  concentration coef1
qui: est store m2_${version}
reg ar  concentration
qui: est store m3_${version}
reg ar  concentration coef3, beta
qui: est store m4_${version}
}
esttab m1_5 m2_5 m3_5 m4_5 using ${tables}/stvalues_all_ar1.tex, label r2 replace star(* 0.10 ** 0.05 *** 0.01)
********************************************************************************
* PARETO TAIL ESTIMATION *******************************************************
********************************************************************************
local trunclist = "10 25 50 100"
foreach trunc of local trunclist{
use ${orig}/historicaldata.dta, clear
keep if year == 2000
rename w93 w93_3
sum employment 
count if employment==.
count if employment==.z
drop if w93_3>=750 & w93_3<900
drop if w93_3<150
forv n = 1(1)$N{
preserve
qui: keep if amr==`n'
qui: keep employment amr estid
qui: gsort -employment 
qui: gen r=_n
qui: gen logrank = ln(r-.5)
qui: gen logsize = ln(employment)
sum
local trunc1 = `trunc'/100*`r(N)'
keep if r<=`trunc1'
qui: reg logrank logsize
qui: mat A=e(b)
qui: gen coef=A[1,1] 
qui: keep coef amr
qui: duplicates drop
if `n'==1{
qui: save "$data_proc/temp.dta", replace
}
else{
qui: append using "$data_proc/temp.dta"
qui: save "$data_proc/temp.dta", replace
}
restore
}
use "$data_proc/temp.dta", clear
saveold "$data_proc/paretotail_`trunc'.dta", replace version(14)
cap erase "$data_proc/temp.dta"
}
********************************************************************************
local listcontrol "1"
foreach cc of local listcontrol{
global version = `cc'
do ${do}/settingglobals.do
use steadystate${version}.dta, clear
keep amr volatility_ss
gen aux = 1
reshape wide volatility_ss, i(aux) j(amr)
drop aux
export delimited using "${inputcali}/vol_avg_estimation.csv", replace novarnames

use citydata_72${version}.dta, clear
keep amr m_empl
gen aux = 1
reshape wide m_empl, i(aux) j(amr)
drop aux
export delimited using "${inputcali}/citysize_avg_estimation.csv", replace novarnames

use citydata_72${version}.dta, clear
qui: replace m_${var_x}=exp(m_${var_x})
qui: gen logsize=ln(m_empl)
qui: gen p99 = m_${var_x}/.01
qui: gen deltasize = -ln(.01)/ln(p99)
qui: keep deltasize logsize amr
qui: duplicates drop
qui: merge 1:1 amr using ${data_proc}/paretotail_10.dta 
qui: drop if _merge!=3
qui: drop _merge
qui: replace coef = -coef
qui: reg deltasize logsize
qui: reg coef logsize
qui: drop logsize
qui: sum
if ${truncation}==1{
drop deltasize
ren coef deltasize
replace deltasize = 1.01 if deltasize<1
}
else{
drop coef
}
gen aux = 1
reshape wide deltasize, i(aux) j(amr)
drop aux 
export delimited "${inputcali}/delta_avg_estimation.csv", replace novarnames
}
********************************************************************************

